package com.orchid.community.models.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.DTO.*;
import com.orchid.community.models.entity.Apply;
import com.orchid.community.models.entity.People;
import com.orchid.community.models.entity.User;
import com.orchid.community.models.mapper.PeopleMapper;
import com.orchid.community.models.mapper.UserMapper;
import com.orchid.community.models.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PeopleMapper peopleMapper;

    @Override
    public MsgResponse getUserList(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPageNo(), userDTO.getPageSize());
        List<UserRequestDTO> pageList = userMapper.getUserList(userDTO.getParam());
        pageList.forEach(userRequestDTO -> {
            String tagss = userRequestDTO.getTagss();
            if (tagss != null) {
                userRequestDTO.setTags(Arrays.stream(tagss.split(", "))
                        .collect(Collectors.toList()));
            }
        });
        PageInfo<UserRequestDTO> pageInfo = new PageInfo<>(pageList);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",  pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return ResultUtils.success(resultMap);
    }

    @Override
    public MsgResponse setTag(CommonParam commonParam) {
        List<String> tags = commonParam.getTags() != null ? commonParam.getTags() : Collections.emptyList();
        String  userId = commonParam.getId();
        if(StrUtil.isEmpty(userId)){
            return ResultUtils.fail("请输入正确的id");
        }
        LocalDateTime now = LocalDateTime.now();
        List<Integer> tagIds = tags.isEmpty() ? null : userMapper.getByTags(tags);
        if(tags.isEmpty()){
            userMapper.deleteByUserNo(userId);
            return ResultUtils.success();
        }
        int count = userMapper.getByUserNo(userId);
        // 根据count和tags的状况决定操作
        if (count == 0) {
            if (tagIds.isEmpty()) {
                // 没有摄像师且没有标签，直接返回成功
                return ResultUtils.success();
            } else {
                // 没有摄像师但有标签，进行插入操作
                userMapper.insertUserTags(userId, tagIds, now);
            }
        } else {
            // 已有摄像师，且有新的标签需要设置
            if (!tagIds.isEmpty()) {
                userMapper.deleteByUserNo(userId);
                userMapper.insertUserTags(userId, tagIds, now);
            }else {
                // 如果count != 0且tagIds为空，返回错误提示，数据库中没有此标签
                return ResultUtils.fail("数据库中没有该标签");
            }
        }
        return ResultUtils.success();
    }

    @Override
    public MsgResponse updateUserInfo(UserInfoDTO users) {
        User user = userMapper.selectByPrimaryKey(users.getUserId());
        if(user == null){
            return ResultUtils.fail("用户不存在");
        }
        user.setUserImage(users.getUserImage());
        user.setNickName(users.getNickName());
        userMapper.update(user);
        return ResultUtils.success("OK");
    }

    @Override
    public MsgResponse getUserInfo(UserInfoDTO users) {
        User user = userMapper.selectByPrimaryKey(users.getUserId());
        return ResultUtils.success(user);
    }

    @Override
    public MsgResponse updateBody(PeopleDTO peopleDTO) {
        People people = peopleMapper.selectByPrimaryKey(peopleDTO.getUserId());
        if(people== null){
            peopleMapper.insert(peopleDTO);
        }else {
            peopleMapper.update(peopleDTO);
        }
        return ResultUtils.success("OK");
    }

    @Override
    public MsgResponse getPeopleInfo(PeopleDTO peopleDTO) {
        People people = peopleMapper.selectByPrimaryKey(peopleDTO.getUserId());
        return ResultUtils.success(people);
    }

    @Override
    public MsgResponse getPeoplePage(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPageNo(), userDTO.getPageSize());
        List<PeopleRequestDTO> pageList = peopleMapper.getPeopleList(userDTO.getParam());
        PageInfo<PeopleRequestDTO> pageInfo = new PageInfo<>(pageList);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",  pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return ResultUtils.success(resultMap);
    }

    @Override
    public MsgResponse apply(Map<String, Object> map) {
        peopleMapper.apply(map);
        return ResultUtils.success("OK");
    }

    @Override
    public MsgResponse getApplyInfo(Map<String, Object> map) {
        Apply applyList = peopleMapper.getApplyInfo(map);
        return ResultUtils.success(applyList);
    }

}
